﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LogicWaveWpf.Common
{
    public class ConfigHelper
    {
        public static T GetConfig<T>(string path,string item)
        {
            T ret = default(T);
            try
            {
                using (StreamReader sr = new StreamReader(path, Encoding.Default))
                {
                    dynamic temp = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(sr.ReadToEnd());
                    object obj = temp;
                    //ret=obj.GetType().GetProperty(item).GetValue(obj).ToString();
                    JObject jo = JObject.Parse(obj.ToString());
                    if (jo.ContainsKey(item)) ret = jo[item].Value<T>();
                }
            }
            catch (Exception) { }
            return ret;
        }

        public static dynamic GetDynamicByJson(string json) => Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);


        public static List<string[]> GetTxtData(string txtPath,string split=";")
        {
            var ret = new List<string[]>();
            using (StreamReader sr = new StreamReader(txtPath, Encoding.UTF8))
            {
                while (!sr.EndOfStream)
                {
                    var line = sr.ReadLine();
                    if (!string.IsNullOrEmpty(line))
                    {
                        var arr = line.Split(split);
                        if (arr?.Length > 0) ret.Add(arr);
                    }
                }
            }
            return ret;
        }


    }
}
